<template>
    <div class="container">
        <van-form>
            <div class="qiye-info">
                <div class="title">人才信息</div>
                <van-cell-group inset>
                    <van-field
                    v-model="form.name"
                    name="name"
                    label="姓名："
                    placeholder=""
                    readonly
                    />
                    <van-field
                    v-model="form.sexLabel"
                    is-link
                    label="性别："
                    placeholder=""
                    readonly
                  />
                  <van-field
                    v-model="form.idCard"
                    is-link
                    label="身份证号："
                    placeholder=""
                    readonly
                  />
                  <van-field
                    v-model="form.nationLabel"
                    is-link
                    label="民族："
                    placeholder=""
                    readonly
                  />
                    <van-field
                    v-model="form.phone"
                    name=""
                    label="联系电话："
                    readonly
                    placeholder=""
                    />
                    <van-field
                    v-model="form.email"
                    name=""
                    label="电子邮件："
                    readonly
                    placeholder=""
                    />
                    <van-field
                    v-model="form.domicileAddress"
                    name=""
                    label="籍贯："
                    readonly
                    placeholder=""
                    />
                    
                    <van-field
                    v-model="form.residentialAddress"
                    name="residentialAddress"
                    label="居住地址："
                    readonly
                    placeholder=""
                    />
                    <van-field
                    v-model="form.companyName"
                    name="residentialAddress"
                    label="所属单位："
                    readonly
                    placeholder=""
                    />
                    <van-field
                    v-model="form.eduLevelLabel"
                    name="eduLevelLabel"
                    label="人才等级："
                    readonly
                    placeholder=""
                    />
                    <van-field
                    v-model="form.occupation"
                    name="occupation"
                    label="现职务："
                    readonly
                    placeholder=""
                    />

                    <van-field
                    v-model="form.postTitleLabel"
                    is-link
                    readonly
                    label="职称："
                    placeholder=""
                  />

                  <van-field
                    v-model="form.eduBackgroundLabel"
                    is-link
                    readonly
                    label="学历："
                    placeholder=""
                  />

                  <van-field
                    v-model="form.eduDegreeLabel"
                    is-link
                    readonly
                    label="学位："
                    placeholder=""
                  />

                  <van-field
                    v-model="form.politicalLabel"
                    is-link
                    readonly
                    label="政治面貌："
                  />
                  <van-field
                    v-model="form.matrimonyLabel"
                    is-link
                    readonly
                    label="婚姻状况："
                  />
                  <van-cell title="合同签订时间：" :value="form.contractStart" />
                  
                  <van-cell title="合同到期时间：" :value="form.contractEnd" />
                  
                  <van-field
                    v-model="form.rdLabel"
                    is-link
                    readonly
                    label="是否人大代表："
                  />

                  <van-field
                    v-model="form.zxLabel"
                    is-link
                    readonly
                    label="是否政协委员："
                  />
                  <van-field
                    v-model="form.partyPresentLabel"
                    is-link
                    readonly
                    label="是否党代表："
                  />
                  <van-field
                  is-link
                  readonly
                  label="在职状态: "
                  v-model="form.jobStatusLabel"
                />
                  <van-field
                  is-link
                  readonly
                  label="人才来源地: "
                  v-model="fieldValue"
                />
                <van-field
                  is-link
                  readonly
                  label="人才头衔: "
                  v-model="form.headTitleLabelList"
                />

                </van-cell-group>
            </div>
            <div class="qiye-info2" v-if="workhistoryList.length">
              <div class="title">工作经历</div>
              <van-cell-group inset v-for="(item, index) of workhistoryList" :key="index">
                <div class="gzjl">工作经历{{index+1}}: </div>
                <van-cell title="开始时间：" :value="item.startDate" />
                <van-cell title="结束时间：" :value="item.endDate" />
                  <van-field
                  v-model="item.companyName"
                  name="companyName"
                  label="公司："
                  placeholder=""
                  readonly
                  />
                  <van-field
                  v-model="item.job"
                  name="job"
                  label="职务："
                  readonly
                  placeholder=""
                  />
                  <div class="title3">工作经历{{index+1}}信息备注：</div>
                  <van-field
                  readonly
                  style="margin-bottom: 2rem;"
                  class="remark"
                  type="textarea"
                  :rows="3"
                  v-model="item.remark"
                  name="remark"
                  label=""
                  placeholder=""
                  />
              </van-cell-group>
            </div>
        </van-form>
    </div>
</template>
 
<script setup>
import { ref } from 'vue';
import { reactive } from 'vue';
import { useRouter,useRoute } from 'vue-router';
import { onMounted } from 'vue'
import { getDictSelect, getOrigin, getTalentDetail, getWorkHistoryList, editTalentPerson, editWorkHistory,creatTalentPerson,creatWorkHistory } from '@/api/company';

const router = useRouter()
const route = useRoute();

const fieldValue = ref('')
// 递归函数,查找省市对应的text
function findTextByValue(originList, value) {
  for (let i = 0; i < originList.length; i++) {
    if (originList[i].enCode === value) {
      return originList[i].fullName;
    }
    if (originList[i].children) {
      const fullName = findTextByValue(originList[i].children, value);
      if (fullName) {
        return fullName;
      }
    }
  }
  return null;
}

//省市
var originList = [];
const getOriginList = () => {
  getOrigin().then(res => {
    const {code,data} = res
    if(code === 200){
      originList = data
      console.log('originList:',originList);
      console.log('provinceId.value22:',provinceId.value);
      console.log('cityId.value222:',cityId.value);
      const value1 = provinceId.value;
      const value2 = cityId.value;
      const text1 = findTextByValue(originList, value1);
      const text2 = findTextByValue(originList, value2);
      console.log(text1); 
      console.log(text2);
      fieldValue.value = text1 + '/' + text2
    }
  })
}

const provinceId = ref('')
const cityId = ref('')
async function getDetail() {
  const id = route.query.personId || '';
  if(id){
    const { code, data } = await getTalentDetail(id);
    if(code === 200) {
      form.name = data.name
      form.idCard = data.idCard
      form.email = data.email
      form.nationLabel = data.nationLabel
      form.sexLabel = data.sexLabel
      form.companyId = data.companyId
      form.domicileAddress = data.domicileAddress
      form.phone = data.phone
      form.residentialAddress = data.residentialAddress
      form.eduLevelLabel = data.eduLevelLabel
      form.occupation = data.occupation
      form.postTitleLabel = data.postTitleLabel
      form.eduBackgroundLabel = data.eduBackgroundLabel
      form.eduDegreeLabel = data.eduDegreeLabel
      form.politicalLabel = data.politicalLabel
      form.companyName = data.talent_company_info?.companyName
      form.rdLabel = data.rdLabel
      form.zxLabel = data.zxLabel
      form.partyPresentLabel = data.partyPresentLabel
      form.headTitleLabelList = data.headTitleLabelList
      form.matrimonyLabel = data.matrimonyLabel
      form.jobStatusLabel = data.jobStatusLabel
      form.contractStart = data.contractStart
      form.contractEnd = data.contractEnd
      provinceId.value = data.province
      cityId.value = data.city
    }
  }
}
const workhistoryList = ref([])
const getWorkHistory = async () => {
  const personId = route.query.personId || '';
  if(personId){
    const { code, data } = await getWorkHistoryList({
      personId: route.query.personId || ''
    });
    if (code === 200 && data.list.length) {
      workhistoryList.value = data.list
    }
  }
}

const form = reactive({
  name: '',
  domicileAddress: '',
  residentialAddress: '',
  occupation: '',
  contractStart: '',
  contractEnd: '',
  phone: '',
  eduLevelLabel: '',
  postTitleLabel: '',
  eduBackgroundLabel: '',
  sexLabel: '',
  politicalLabel: '',
  rdLabel: '',
  zxLabel: '',
  province: '',
  city: '',
  companyId: localStorage.getItem('companyId'),
})

onMounted(() => {
  getDetail()
  getWorkHistory()
  setTimeout(()=>{
    getOriginList()
  },1000)
})
</script>
 

<style lang="scss" scoped>
.container {
  background: #f9f9f9;
  overflow: hidden;
  // margin-bottom: 45px;
  padding: 0.9rem 1.3rem;
}
.navbar {
    position: fixed;
    top: 0;
    width: 100%;
    background-color: #fff;
}
.addbtn{
  position: fixed;
  bottom: 2rem;
  width: 92%;
  left: 4%;
}
.van-form{
  padding: 1.4rem 0 0 0;
}
.van-cell-group{
  background: #fff;
}
.van-field{
 border-bottom: 1px solid #E0E0E4;
 padding: 2rem 0 1.6rem;
}
.title{
    font-size: 1.8rem;
    font-weight: 700;
    margin: 0 0 0.5rem 1.2rem;
}
.qiye-info,.qiye-info2{
    background: #fff;
    padding-top: 1.2rem;
}
.qiye-info2{
  margin-top: 2rem;
}
.qita{
    margin-top: 2rem;
    padding: 1.2rem;
    background: #fff;
    margin-bottom: 0.6rem;
}
.title2{
    font-size: 1.8rem;
    font-weight: 700;
    margin: 0 0 0.5rem 0;
}
.title3{
    font-size: 1.4rem;
    font-weight: 400;
    margin: 2.5rem 0 1.2rem 0;
}
.remark{
    border: 1px solid #E0E0E4;
    padding: 0.4rem 1rem!important;
}

.van-button{
  font-size: 1.8rem;
  font-weight: 400;
}
.van-cell{
  border-bottom: 1px solid #E0E0E4;
  padding: 2rem 0 1.6rem;
}
::v-deep .van-cell__title{
  width: 10rem !important;
  flex: none;
  margin-right: 1.2rem;
}
::v-deep .van-cell__value{
  text-align: left;
  color: #323233;
}
::v-deep .van-cell__right-icon{
  display: none;
}
::v-deep .van-cell-group{
  margin-bottom: 0.5rem;
}
.gzjl{
  font-size: 1.4rem;
  font-weight: 700;
  margin-top: 1rem;
}

::v-deep .van-field__label{
  width: 10rem!important;
}

</style>